﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// AnniLaborCalendar 的摘要说明
/// </summary>
public class AnniLaborCalendar
{
    Dictionary<int, List<int>[]> _calendarData;

    public Dictionary<int, List<int>[]> CalendarData
    {
        get { return _calendarData; }
    }

	public AnniLaborCalendar()
	{
        initCalendarData();
	}

    private void initCalendarData()
    {
        _calendarData = new Dictionary<int, List<int>[]>();

        // 2013 PSH 年历；每年追加此处年历数据
        List<int>[] anniData2013 = new List<int>[12];
        anniData2013[0] = new List<int> { 4, 5, 7, 8, 9, 10, 11, 14, 15, 16, 17, 18, 21, 22, 23, 24, 25, 28, 29, 30, 31 };
        anniData2013[1] = new List<int> { 1, 4, 5, 6, 7, 8, 17, 18, 19, 20, 21, 22, 25, 26, 27, 28 };
        anniData2013[2] = new List<int> { 1, 4, 5, 6, 7, 8, 11, 12, 13, 14, 15, 18, 19, 20, 21, 22, 25, 26, 27, 28, 29 };
        anniData2013[3] = new List<int> { 1, 2, 3, 7, 8, 9, 10, 11, 12, 15, 16, 17, 18, 19, 22, 23, 24, 25, 26, 27 };
        anniData2013[4] = new List<int> { 2, 3, 6, 7, 8, 9, 10, 13, 14, 15, 16, 17, 20, 21, 22, 23, 24, 27, 28, 29, 30, 31 };
        anniData2013[5] = new List<int> { 3, 4, 5, 6, 7, 8, 9, 13, 14, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28 };
        anniData2013[6] = new List<int> { 1, 2, 3, 4, 5, 8, 9, 10, 11, 12, 15, 16, 17, 18, 19, 22, 23, 24, 25, 26, 29, 30, 31 };
        anniData2013[7] = new List<int> { 1, 2, 5, 6, 7, 8, 9, 12, 13, 14, 15, 16, 19, 20, 21, 22, 23, 26, 27, 28, 29, 30 };
        anniData2013[8] = new List<int> { 2, 3, 4, 5, 6, 9, 10, 11, 12, 13, 16, 17, 18, 22, 23, 24, 25, 26, 27, 29, 30 };
        anniData2013[9] = new List<int> { 8, 9, 10, 11, 12, 14, 15, 16, 17, 18, 21, 22, 23, 24, 25, 28, 29, 30, 31 };
        anniData2013[10] = new List<int> { 1, 4, 5, 6, 7, 8, 11, 12, 13, 14, 15, 18, 19, 20, 21, 22, 25, 26, 27, 28, 29 };
        anniData2013[11] = new List<int> { 2, 3, 4, 5, 6, 9, 10, 11, 12, 13, 16, 17, 18, 19, 20, 23, 24, 25, 26, 27, 30, 31 };
        _calendarData.Add(2013, anniData2013);

        List<int>[] anniData2014 = new List<int>[12];
        anniData2014[0] = new List<int> { 2, 3, 6, 7, 8, 9, 10, 13, 14, 15, 16, 17, 20, 21, 22, 23, 24, 26, 27, 28, 29 };
        anniData2014[1] = new List<int> { 7, 8, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28 };
        anniData2014[2] = new List<int> { 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 31 };
        anniData2014[3] = new List<int> { 1, 2, 3, 4, 8, 9, 10, 11, 14, 15, 16, 17, 18, 21, 22, 23, 24, 25, 28, 29, 30 };
        anniData2014[4] = new List<int> { 5, 6, 7, 8, 9, 12, 13, 14, 15, 16, 19, 20, 21, 22, 23, 26, 27, 28, 29, 30 };
        anniData2014[5] = new List<int> { 3, 4, 5, 6, 9, 10, 11, 12, 13, 16, 17, 18, 19, 20, 23, 24, 25, 26, 27, 30 };
        anniData2014[6] = new List<int> { 1, 2, 3, 4, 7, 8, 9, 10, 11, 14, 15, 16, 17, 18, 21, 22, 23, 24, 25, 28, 29, 30, 31 };
        anniData2014[7] = new List<int> { 1, 4, 5, 6, 7, 8, 11, 12, 13, 14, 15, 18, 19, 20, 21, 22, 25, 26, 27, 28, 29 };
        anniData2014[8] = new List<int> { 1, 2, 3, 4, 5, 9, 10, 11, 12, 15, 16, 17, 18, 19, 22, 23, 24, 25, 26, 28, 29, 30 };
        anniData2014[9] = new List<int> { 8, 9, 10, 11, 13, 14, 15, 16, 17, 20, 21, 22, 23, 24, 27, 28, 29, 30, 31 };
        anniData2014[10] = new List<int> { 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28 };
        anniData2014[11] = new List<int> { 1, 2, 3, 4, 5, 8, 9, 10, 11, 12, 15, 16, 17, 18, 19, 22, 23, 24, 25, 26, 29, 30, 31 };
        _calendarData.Add(2014, anniData2014);
    }

    /// <summary>
    /// only for test days of a year
    /// </summary>
    public int WorkDaysCount(int year)
    {
        int count = 0;
        for (int i = 0; i < 12; i++)
        {
            count += _calendarData[year][i].Count;
        }
        return count;
    }

    public void CreateManMonths(ManMonths manMonths)
    {
        DateTime start = manMonths.Start;
        DateTime end = manMonths.End;
        manMonths.StartMonthDays = 0;
        manMonths.EndMonthDays = 0;
        manMonths.MiddleMonths = 0;
        manMonths.TotleManMonths = 0;

        double manmonths = 0;
        int imonths = (end.Year - start.Year) * 12 + (end.Month - start.Month);
        switch (imonths)
        {
            case 0:
                // 单个月
                manmonths = manmonthsConvert(monthCalendar(start), start.Day, end.Day);
                manMonths.StartMonthDays = WorkDaysCount(monthCalendar(start), start.Day, end.Day);
                manMonths.EndMonthDays = manMonths.StartMonthDays;
                manMonths.TotleDays = manMonths.StartMonthDays;
                break;
            case 1:
                int lastday1 = DateTime.DaysInMonth(start.Year, start.Month);
                // 第一个月
                manmonths = manmonthsConvert(monthCalendar(start), start.Day, lastday1);
                manMonths.StartMonthDays = WorkDaysCount(monthCalendar(start), start.Day, lastday1);
                // 第二个月
                manmonths += manmonthsConvert(monthCalendar(end), 1, end.Day);
                manMonths.EndMonthDays = WorkDaysCount(monthCalendar(end), 1, end.Day);
                manMonths.TotleDays = manMonths.StartMonthDays + manMonths.EndMonthDays;
                break;
            default:
                int lastday2 = DateTime.DaysInMonth(start.Year, start.Month);
                // 首月
                manmonths = manmonthsConvert(monthCalendar(start), start.Day, lastday2);
                manMonths.StartMonthDays = WorkDaysCount(monthCalendar(start), start.Day, lastday2);
                // 中间整月
                manmonths += imonths - 1;
                manMonths.MiddleMonths = imonths - 1;
                // 末月
                manmonths += manmonthsConvert(monthCalendar(end), 1, end.Day);
                manMonths.EndMonthDays = WorkDaysCount(monthCalendar(end), 1, end.Day);

                int days = 0;
                for (int i = 0, year, month; i < imonths - 1; i++)
                {
                    year = start.Year + (start.Month + i) / 12;
                    month = (start.Month + i) % 12;
                    days += _calendarData[year][month].Count;
                }
                manMonths.TotleDays = manMonths.StartMonthDays + manMonths.EndMonthDays + days;

                break;
        }
        manMonths.TotleManMonths = manmonths;
    }

    /// <summary>
    /// 某日期所在月的工作月历
    /// </summary>
    /// <param name="date">日期</param>
    /// <returns>月历List</returns>
    private List<int> monthCalendar(DateTime date)
    {
        List<int> monthData = _calendarData[date.Year][date.Month - 1];
        return monthData;
    }
    
    private double manmonthsConvert(List<int> monthCalendar, int day1, int day2)
    {
        int count = WorkDaysCount(monthCalendar, day1, day2);

        // 工作日数除以月工作日
        return (double)count / (double)monthCalendar.Count();
    }

    /// <summary>
    /// 某月两天之间的工作日
    /// </summary>
    private int WorkDaysCount(List<int> monthCalendar, int day1, int day2)
    {
        int count = 0;
        foreach (int i in monthCalendar)
        {
            if (i >= day1 && i <= day2)
            {
                count++;
            }
        }
        return count;
    }

}